<!DOCTYPE html>
<!--
/*************************************************************************
* Copyright (c) 2011 im007boy@gmail.com. All rights reserved.            *
* Use of this source code is governed by a GNU-style license that can be *
* found in the LICENSE file.                                             *
*************************************************************************/
-->

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript" src="js/analytics.js"></script>
<title>豌豆荚</title>
<script>
var videoRegExp = new RegExp('^http:\\/\\/(www.56.com\\/|v.youku.com\\/|v.ku6.com\\/|www.umiwi.com\\/video\\/|www.youtube.com\\/watch|video.sina.com.cn\\/)');
var currentView = {
	'tabId':-2,
	'windowId':-2
	};
var contextMenu = {
	'ready':false,
	'id':0,
	'contexts':['link','page'],
	'onClick':wandou_dl
	};
function wandou_dl(info,tab){
	var url = info.linkUrl||info.pageUrl; 
	var type = 'apk';
	var name = '下载_'+new Date().getHours()+'.'+new Date().getMinutes();
	var wandou_url = 'blank.html';
	
	if (tab.title)
		name = tab.title.slice(0,24);
	if (url && (url.indexOf('http') > -1 || url.indexOf('ftp') > -1)){
		if (videoRegExp.test(url)){
			//视频
			type = 'video';
		}
		//video apk 暂不支持电子书和音乐
		wandou_url = 'wandoujia://type='+type+'&name='+name+'&url='+url+'==';
		debug(name,url);
		//start download
		document.getElementById('wandou_iframe').innerHTML = '<iframe style="display:none;" src="' + wandou_url + '"></iframe>';
		if (localStorage.getItem("auto_report") == "false")
			ga_push('下载_'+type,'这是一个不为人知的网址');
		else
			ga_push('下载_'+type,get_domain(url));
	}else{
		ga_push('错误',"未知目标地址");
	}
}	
function contextMenusInit(){
	contextMenu.id = chrome.contextMenus.create({
		'type':'normal',
		'title':'豌豆荚安装器',
		'contexts':contextMenu.contexts,
		'onclick':function(info,tab){contextMenu.onClick(info,tab)},
		}, function(){
			contextMenu.ready = true;
			
			//tab view changed
			chrome.tabs.onSelectionChanged.addListener(function(tabId, selectInfo) {
				if (tabId != currentView.tabId || windowId != selectInfo.windowId){
					currentView.tabId = tabId;
					currentView.windowId = selectInfo.windowId;
					//update contextMenu;
					contextMenusUpdate();
				}
			});
			//tab url changed
			chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {
				if (tabId == currentView.tabId)
					contextMenusUpdate();
			});
		});
} 
function contextMenusUpdate(){
	chrome.tabs.get(currentView.tabId,function(tab){
		if (videoRegExp.test(tab.url))
			contextMenu.contexts = ['link','page'];
		else
			contextMenu.contexts = ['link'];
		chrome.contextMenus.update(contextMenu.id,{'contexts':contextMenu.contexts});
	});
}

function welcome(){
	chrome.tabs.create({
		'url':chrome.extension.getURL('options.html')
	})
}
function onload(){
	if (!localStorage.getItem('auto_report'))
		localStorage.setItem('auto_report','true');
		
	chrome.tabs.getSelected(null, function(e){
		currentView.tabId = e.id;
		currentView.windowId = e.windowId;
		contextMenusInit();
	});	
	
	if (navigator.userAgent.indexOf("ChromePlus") == -1 && navigator.userAgent.indexOf("SunChrome") == -1){
		if(!localStorage["version"]){
			welcome();
			ga_push("[Welcome]", location.hostname, navigator.userAgent, 0);
			localStorage["version"] = "1.0.0";
		}
	}
	var ext = new XMLHttpRequest();
	ext.onreadystatechange = function() {
		if (ext.readyState == 4) {
			try{
				if (JSON.parse(ext.responseText)){
					_extension = JSON.parse(ext.responseText);
					localStorage["version"] = _extension.version;
					if (_gaq != null && typeof _gaq != "undefined")
						_gaq.push(['_setCustomVar', 1,'version', _extension.version, 1]);
				}
			}catch(e){}
		}
	}	
	try{
		ext.open("GET", "./manifest.json", true);
		ext.send();
	}catch(e){
		;
	}	
	setTimeout(function(){
		ga_push('Version',localStorage["version"]);
	},2000);		

}
function debug(){
	var i = 0;
	if (!localStorage.getItem('debug'))
		return;
	for (i = 0;i < arguments.length;i++){
		console.log(arguments[i]);
	}
}
function get_domain(str){
	var domain = "undefined";
	if (str.toLowerCase().indexOf('http://') == 0){
		domain = str.substring(7);
		domain = 'http://'+domain.substring(0,domain.indexOf('/'));
	}else if (str.toLowerCase().indexOf('https://') == 0){
		domain = str.substring(8);
		domain = 'https://'+domain.substring(0,domain.indexOf('/'));
	}
	return domain;
}

</script>
</head>
<body onload = "onload()">
<div id="wandou_iframe" style="display:none;"></div>
</body>
</html>
